package com.runshu.runshuwulian.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;

import androidx.appcompat.widget.AppCompatRadioButton;

import com.hyphenate.easeui.mwidget.mutil.MUtils;

public class YRadioButton extends AppCompatRadioButton {
    int noticeNum = 0;
    float textSize = (float) 9;
    float padding = 4;
    float marginTop = 8;
    float toCenter = 10;

    public YRadioButton(Context context) {
        super( context );
    }

    public YRadioButton(Context context, AttributeSet attrs) {
        super( context, attrs );
    }

    public YRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super( context, attrs, defStyleAttr );
    }

    Paint pen;
    Paint textPen;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw( canvas );
        if (noticeNum == 0) return;
        if (pen == null) {
            pen = new Paint(Paint.ANTI_ALIAS_FLAG);
            pen.setColor( 0xffff3600 );
            pen.setStrokeCap( Paint.Cap.ROUND );
            pen.setStrokeWidth( (float) MUtils.dip2px( getContext(), textSize + padding ) );
        }
        if (textPen == null) {
            textPen = new Paint(Paint.ANTI_ALIAS_FLAG);
            textPen.setTextSize( MUtils.dip2px( getContext(), textSize ) );
            textPen.setColor( 0xffffffff );
        }
        String num = String.valueOf( noticeNum );
        float textWith = textPen.measureText( num );
        float textSingSize = textPen.measureText( "0" );
        Log.v( "YRadioButton", num );
        float pxPadding = getPadding( padding );
        int width = getWidth();
        float center = width / 2 + getPadding( toCenter );
//        float v = textWith / 2;
//        float v = (textWith -pen.getStrokeWidth())/ 2;
        float v = (textWith -pen.getStrokeWidth()+pxPadding)/ 2;
        if(v<=0) v= (float) 0.01;
        canvas.drawLine( center - v, MUtils.dip2px( getContext(), marginTop ), center + v, MUtils.dip2px( getContext(), marginTop ), pen );
        canvas.drawText( num, center - textWith / 2, MUtils.dip2px( getContext(), marginTop + padding - padding / 5 ), textPen );
    }

    private float getPadding(float padding) {
        return MUtils.dip2px( getContext(), padding );
    }

    public void setNoticeNum(int noticeNum) {
        this.noticeNum = noticeNum;
        invalidate();
    }

    public int getNoticeNum() {
        return noticeNum;
    }

    public void clearNoticeNum() {
        noticeNum = 0;
        invalidate();
    }
}
